const app = {
	state: {
    // 已经访问过的sideer的path
		visitedViews: [],
		savedData: {}
	},
	mutations: {
    // 添加以访问过信息
		ADD_VISITED_VIEWS: (state, view) => {
			if (state.visitedViews.some(v => v.path === view.path)
      ) return
			state.visitedViews.push({ name: view.name, path: view.path })
		},
    // 删除访问信息
		DEL_VISITED_VIEWS: (state, view) => {
			let index
      // 遍历已访问信息，找到待删除信息的index
			for (const [i, v] of state.visitedViews.entries()) {
				if (v.path === view.path) {
					index = i
					break
				}
			}
      // 删除信息
			state.visitedViews.splice(index, 1)
		},
		ADD_ITEMS: (state, data) => {
			state.savedData[data.key] = data.value
		},
		REMOVE_ITEMS: (state, key) => {
			state.savedData[key] = null
			delete state.savedData[key]
		},
		GET_ITEMS: (state, key) => {
			if (state.savedData[key]) {
				return state.savedData[key]
			}
			return null
		}
	},
	actions: {
		addVisitedViews({ commit }, view) {
			commit('ADD_VISITED_VIEWS', view)
		},
		delVisitedViews({ commit, state }, view) {
      // 返回删除后的visitedViews
			return new Promise((resolve) => {
        // 清除存储数据
				commit('REMOVE_ITEMS', view.path)
				commit('DEL_VISITED_VIEWS', view)
				resolve([...state.visitedViews])
			})
		},
		addItems({ commit }, data) {
			commit('ADD_ITEMS', data)
		},
		getItems({ commit }, key) {
			commit('GET_ITEMS', key)
		}
	}
}

export default app
